import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/widgets/bottom_input_widget.dart';

import '../../../config/global_configs.dart';
import '../../../net/response/food/regular_week_menu_list_data.dart';
import '../../../net/response/food/week_menu_list_data.dart';
import '../../../res/colors.dart';
import '../../../viewmodel/food/new_week/new_regular_week_submit_view_model.dart';

class NewRegularWeekSubmitPage extends BaseStatefulMvvmWidget {
  RegularWeekMenuListData menuData;
  final String oldId;
  final String sitId;
  final String time;
  final String orderModelType;
  final String useType;

  NewRegularWeekSubmitPage(
      {required this.menuData,
      required this.oldId,
      required this.sitId,
      required this.time,
      required this.orderModelType,
      required this.useType});

  @override
  State<StatefulWidget> createState() => _NewRegularWeekSubmitState();
}

class _NewRegularWeekSubmitState extends BaseMvvmState<
    NewRegularWeekSubmitViewModel, NewRegularWeekSubmitPage> {
  @override
  Widget buildLoadingContentView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ListView(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('已点项目', textColor: Colors.black, textSize: 18),
                Container(
                  height: 10,
                ),
                ListView.separated(
                  itemBuilder: (context, index) {
                    var item = viewModel.selectedMealList[index];
                    return selectMealItem(item);
                  },
                  separatorBuilder: (context, index) {
                    return Gaps.dividerLine;
                  },
                  itemCount: viewModel.selectedMealList.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                )
              ],
            ).addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: 10.borderRadius)),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('备注信息', textColor: Colors.black, textSize: 18),
                Container(
                  height: 10,
                ),
                CommonText(viewModel.remark, hintText: '输入备注信息')
                    .addToContainer(
                        decoration: BoxDecoration(
                            color: Colours.bg_color,
                            borderRadius: 10.borderRadius),
                        padding: EdgeInsets.all(8),
                        width: MediaQuery.of(context).size.width,
                        constraints: BoxConstraints(minHeight: 100))
                    .onTap(() {
                  showBottomInput(context,
                      title: "备注信息",
                      content: viewModel.remark, onSubmit: (str) {
                    setState(() {
                      viewModel.remark = str;
                    });
                  });
                })
              ],
            ).addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: 10.borderRadius)),
          ],
        ).setWeight(1),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            if(GlobalConfigs.showPayPrice)
            CommonText('合计 '),
            GlobalConfigs.showPayPrice?
            CommonText("¥${viewModel.totalPrice - viewModel.oldPrice}",
                textSize: 18):CommonText(""),
            Container(width: 16,),
            TextButton(
              onPressed: () {
                viewModel.submitRequest();
              },
              child: CommonText('确认提交', textColor: Colors.white),
              style: ButtonStyle(
                backgroundColor:
                    WidgetStateProperty.all(Colours.foodSelectColor),
                shape: WidgetStateProperty.all(StadiumBorder()),
                padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 18))
              ),
            ),
            Container(width: 16,),
          ],
        ).addToContainer(height: 49, color: Colors.white)
      ],
    );
  }

  Widget selectMealItem(GoodsInfo info) {
    var paidList =
        (info.goodsList??[]).where((element) => element.oldStatus == 1).toList();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 8,
        ),
        Row(
          children: [
            CommonText(
                    "${info.weekDate?.toDateStr([
                          'm',
                          '月',
                          'd',
                          '日'
                        ])}  ${info.timeTypeId?.timeTypeStr()}",
                    textSize: 17,
                    textColor: Colors.grey[700]!,
                    fontWeight: FontWeight.bold)
                .setWeight(1),
            CommonText("¥${info.selectPrice() - info.paidPrice()}",
                textSize: 17, textColor: Colors.red),
          ],
        ),
        Container(
          height: 15,
        ),
        CommonText(
            info.selectItems.map((e) => "${e.goodsName}x${e.number}").join("、"),
            textColor: Colors.grey[800]!,
            textSize: 17),
        Container(
          height: 15,
        ),
        if (paidList.isNotEmpty) ...[
          CommonText(
            '已结算',
            textColor: Colors.grey,
          ),
          Container(
            height: 15,
          ),
          ...paidList
              .map((e) => Row(
                    children: [
                      Container(
                        width: 40,
                      ),
                      CommonText(e.goodsName, textColor: Colors.grey)
                          .setWeight(2),
                      CommonText("x${e.number}",
                              textColor: Colors.grey,
                              textAlign: TextAlign.center)
                          .setWeight(1),
                      CommonText("¥${e.price}",
                              textColor: Colors.grey, textAlign: TextAlign.end)
                          .setWeight(1),
                    ],
                  ))
              .toList(),
          Container(
            height: 15,
          ),
        ]
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("提交订单"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  NewRegularWeekSubmitViewModel createViewModel() {
    return NewRegularWeekSubmitViewModel(context,
        menuData: widget.menuData,
        oldId: widget.oldId,
        sitId: widget.sitId,
        time: widget.time,
        orderModelType: widget.orderModelType,
        useType: widget.useType);
  }

  @override
  void onRetryClick() {}
}
